﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SharedGenomics.Domain
{
    public class DataSchema : IEnumerable<DataColumnTypeBase>
    {
        #region Private Members

        private KeyedColumnCollection _columns;
        //private IList<string> _columnNames;

        #endregion

        #region Constructors

        public DataSchema()
        {
            this._columns = new KeyedColumnCollection();
            //this._columnNames = new List<string>();
        }

        #endregion

        #region Public Methods

        public void AddColumn(DataColumnTypeBase column)
        {
            if (column == null)
                throw new ArgumentNullException();

            if (this._columns.Contains(column.Name))
                throw new ArgumentException(String.Format("A column with the name '{0}' already exists in the collection", column.Name));

            column.Index = this._columns.Count;
            this._columns.Add(column);
            
            //this._columnNames.Add(column.Name);
        }

        #endregion

        #region Properties

        public DataColumnTypeBase this[int index]
        {
            get
            {
                if (index < 0 || index >= this._columns.Count)
                    throw new ArgumentOutOfRangeException("Index must fall within the bounds of the collection");

                return this._columns[index];
            }
        }

        public DataColumnTypeBase this[string columnName]
        {
            get
            {
                if (this._columns.Contains(columnName))
                    return this._columns[columnName];
                else
                    throw new ArgumentException(String.Format("No column with name {0} was found in the collection", columnName));
            }
        }

        public int Count
        {
            get { return this._columns.Count; }
        }

        #endregion

        #region IEnumerable<DataColumnTypeBase> Members

        public IEnumerator<DataColumnTypeBase> GetEnumerator()
        {
            return this._columns.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }

        #endregion

        #region KeyedColumnCollection

        private class KeyedColumnCollection : System.Collections.ObjectModel.KeyedCollection<string, DataColumnTypeBase>
        {
            protected override string GetKeyForItem(DataColumnTypeBase item)
            {
                return item.Name;
            }
        }

        #endregion
    }
}
